import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'

import { store } from '../store'
import { loginToken } from '@/ajax/api/messages/marketing/loginToken'

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to: any, from: any, next: any) => {
  window.document.title = to.meta.title

  next()
  
  // const smsToken = store.getters['common/getSmsToken']

  // if (smsToken) {
  //   next()
  // } else {
  //   loginToken({ code: to.query.code }).then((res: any) => {
  //     store.dispatch('common/setSmsToken', res.sms_token || '').then(() => {
  //       next()
  //     })
  //   })
  // }
})

router.onError((err, to, from) => {
  console.log(to.fullPath, '--to\n', from.fullPath, '--form\n', JSON.parse(JSON.stringify(err)), '\n', '路由错误')
  const jsModulePattern = /dynamically imported module/g
  const jsLoadPattern = /Failed to load module script/g
  const isFailed = err.message.match(jsModulePattern || jsLoadPattern)
  if (isFailed) window.location.reload()
})

export default router
